/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmlreader;

import filetransserv.MessageHandler;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.util.Iterator;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

/**
 *
 * @author Oscar
 */
public class CommandInterpreter {

    private MessageHandler Handler;

    public CommandInterpreter() {

    }

    public CommandInterpreter(MessageHandler handler) {

        this.Handler = handler;
    }

    /**
     * Interpreta un mensaje XML recibido por el servidor
     *
     * @param configFile
     * @return
     */
    @SuppressWarnings({ "unchecked", "null" })
    public void readCommand(InputStream command) {

        try {

            // Fabrica de entrada de XML
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();

            // Se crea un nuevo eventReader
            XMLEventReader eventReader = inputFactory.createXMLEventReader(command);

            while (eventReader.hasNext()) {

                XMLEvent event = eventReader.nextEvent();

                if (event.isStartElement()) {

                    StartElement startElement = event.asStartElement();

                    // Si tenemos un comando authenticate
                    if (startElement.getName().getLocalPart().equals("authenticate")) {

                        CommandAuthenticate(startElement);
                    }

                    // Si tenemos un comando register
                    if (startElement.getName().getLocalPart().equals("register")) {

                        CommandRegister(startElement);
                    }

                    // Si tenemos un comando getLoad
                    if (startElement.getName().getLocalPart().equals("getload")) {

                        CommandGetLoad(startElement);
                    }

                    // Si tenemos un comando getFiles
                    if (startElement.getName().getLocalPart().equals("getfiles")) {

                        CommandGetFiles(startElement);
                    }

                    // Si tenemos un comando getFiles
                    if (startElement.getName().getLocalPart().equals("download")) {

                        CommandDownload(startElement);
                    }

                }

            }

        }
        catch (XMLStreamException e) {
        }
    }

    /**
     * Llama a la funcion asociada al tag XML
     *
     * @param startElement Tag XML
     */
    public void CommandAuthenticate (StartElement startElement) throws XMLStreamException{

        
        String nickname = null;
        String password = null;

        // Se leen los atributos del comando
        Iterator<Attribute> attributes = startElement
                        .getAttributes();

        while (attributes.hasNext()) {

            Attribute attribute = attributes.next();

            if (attribute.getName().toString().equals("nickname")) {

                nickname = attribute.getValue();
            }

            if (attribute.getName().toString().equals("password")) {

                password = attribute.getValue();
            }

        }

        Handler.ActualizarConsola
                ("Autenticar: nickname=\"" + nickname + "\" password=\"" + password + "\"");

        Handler.getManager().authenticate
                (Handler.getSocket(),nickname, password);
        
    }

    public void CommandRegister (StartElement startElement) throws XMLStreamException{


        String nickname = null;
        String password = null;

        // Se leen los atributos del comando
        Iterator<Attribute> attributes = startElement
                        .getAttributes();

        while (attributes.hasNext()) {

            Attribute attribute = attributes.next();

            if (attribute.getName().toString().equals("nickname")) {

                nickname = attribute.getValue();
            }

            if (attribute.getName().toString().equals("password")) {

                password = attribute.getValue();
            }

        }

        Handler.ActualizarConsola
                ("Registar: nickname=\"" + nickname + "\" password=\"" + password + "\"");

        Handler.getManager().register
                (Handler.getSocket(),nickname, password);

    }

    public void CommandGetLoad (StartElement startElement) throws XMLStreamException{

        Handler.ActualizarConsola
                ("Recibido: GetLoad");

        Handler.getManager().getLoad
                (Handler.getSocket());

    }


    public void CommandGetFiles (StartElement startElement) throws XMLStreamException{

        Handler.ActualizarConsola
                ("Recibido: GetFiles");

        Handler.getManager().getFiles
                (Handler.getSocket());

    }


    public void CommandDownload (StartElement startElement){

        String nickname = null;
        String file = null;
        long size = 0;
        int block = 0;

        // Se leen los atributos del comando
        Iterator<Attribute> attributes = startElement
                        .getAttributes();

        while (attributes.hasNext()) {

            Attribute attribute = attributes.next();

            if (attribute.getName().toString().equals("user")) {

                nickname = attribute.getValue();
            }
            if (attribute.getName().toString().equals("name")) {

                file = attribute.getValue();
            }
            if (attribute.getName().toString().equals("block")) {

                block = Integer.parseInt(attribute.getValue());
            }

        }

        Handler.ActualizarConsola
                ("Recibido: Download " + file);

        Handler.getManager().Download
                (Handler.getSocket(), nickname, file, block);

    }


}
